package com.openness.spider.template.netbase;

import java.text.SimpleDateFormat;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openness.spider.Html;
import com.openness.spider.commons.Statics;
import com.openness.spider.template.Template;

/**
 * @author Rain
 * @version 2013-6-26 下午4:29:16
 */

public class NewsTemplate implements Template {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(NewsTemplate.class);

	private static final SimpleDateFormat SDF = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	@Override
	public Html parse(String url, String source) throws Exception {
		Document document = Jsoup.parse(source);

		String htmlTitle = null;

		String htmlKeywords = null;

		String htmlDescription = null;

		String textTitle = null;

		String textContent = null;

		String textSource = null;

		String textTime = null;

		try {
			Elements titleElements = document.getElementsByTag("title");
			if (titleElements != null && titleElements.size() > 0) {
				htmlTitle = titleElements.get(0).text();
			}

			Elements metaElements = document.getElementsByTag("meta");
			if (metaElements != null && metaElements.size() > 0) {
				for (Element metaElement : metaElements) {
					if (metaElement.hasAttr("name")) {
						String name = metaElement.attr("name");

						if (name.equalsIgnoreCase("keywords")) {
							htmlKeywords = metaElement.attr("content");
						} else if (name.equalsIgnoreCase("description")) {
							htmlDescription = metaElement.attr("content");
						}
					}
				}
			}

			Element textTitleElement = document.getElementById("h1title");
			if (textTitleElement != null) {
				textTitle = textTitleElement.text();
			}

			Elements textContentElements = document
					.getElementsByAttributeValue("id", "endText");
			if (textContentElements != null) {
				Elements pElements = textContentElements.get(0)
						.getElementsByTag("p");
				if (pElements != null && pElements.size() > 0) {
					StringBuilder sb = new StringBuilder();

					for (Element pElement : pElements) {
						sb.append(pElement.text().trim() + "\n");
					}

					textContent = sb.toString();
				}
			}

			Elements textSourceElements = document.getElementsByAttributeValue(
					"class", "left");
			if (textSourceElements != null && textSourceElements.size() > 0) {
				Elements aElements = textSourceElements.get(0)
						.getElementsByTag("a");
				if (aElements != null && aElements.size() > 0) {
					textSource = aElements.get(0).text();
				}
			}

			Elements textTimeElements = document.getElementsByAttributeValue(
					"class", "left");
			if (textTimeElements != null && textTimeElements.size() > 0) {
				String text = textTimeElements.get(0).text();

				int end = text.indexOf("　来源:");

				textTime = Statics.TIME_FORMAT.format(SDF.parse(text.substring(
						0, end)));
			}
		} catch (Exception e) {
			LOGGER.error("NewsTemplate parse error: " + e.toString());
		}

		return new Html(url, source, htmlTitle, htmlKeywords, htmlDescription,
				textTitle, textContent, textSource, textTime);
	}

	public static void main(String[] args) throws Exception {
		String url = "http://news.163.com/13/0628/18/92FOVO8Q0001124J.html";

		String source = Jsoup.connect(url).get().html();

		Html html = new NewsTemplate().parse(url, source);

		System.out.println(html.toString());
	}

}
